package aps.module_Machines;

//Java
import java.util.LinkedList;

//Vanilla Minecraft
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraft.src.ModLoader;
import net.minecraft.src.Block;
import net.minecraftforge.common.Configuration;

//Buildcraft
import buildcraft.api.core.BuildCraftAPI;
import buildcraft.api.gates.Action;
import buildcraft.api.gates.ActionManager;
import buildcraft.api.gates.Trigger;
import buildcraft.api.recipes.AssemblyRecipe;
import buildcraft.BuildCraftCore;
import buildcraft.BuildCraftEnergy;
import buildcraft.BuildCraftSilicon;
import buildcraft.BuildCraftTransport;

//APS
import aps.BuildcraftAPS;
import aps.module;
import aps.module_Converter.module_Converter;
import aps.module_Core.module_Core;
import aps.module_Core.module_Core.APSBlockTypes;



public class module_Machines extends module
{	
	private int EnergyStoreID = 0;
	private int EnergyDirectorID = 1;
	private int ExtractorID = 2;
	private int GrinderID = 3;
	private int MagmafierID = 4;
	private int PoweredFurnaceID = 5;
	private int EnergySiphonID = 6;
	
	public static Trigger energyStoreFullTrigger;
	public static Trigger energyStoreEmptyTrigger;
	public static Trigger energyStorePartFilledTrigger;
	public static Trigger BlastFurnaceFullTrigger;
	public static Trigger BlastFurnaceEmptyTrigger;
	public static Trigger BlastFurnacePartFilledTrigger;
	
	public static Action directorDisableNorth;
	public static Action directorDisableSouth;
	public static Action directorDisableEast;
	public static Action directorDisableWest;
	public static Action directorEnableNorth;
	public static Action directorEnableSouth;
	public static Action directorEnableEast;
	public static Action directorEnableWest;
	
	public static LinkedList<GrinderRecipe> grinderRecipes = new LinkedList<GrinderRecipe>();
	public static LinkedList<Block> MeltableBlocks = new LinkedList<Block>();
	public static GrinderRecipe ExtractorProducts;
	
	
	
	public module_Machines() {}

	@Override
	public void preInit() {
	}
	
	@Override
	public void initialize()
	{	
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Block.gravel), new ItemStack(Item.flint), 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.flint), new ItemStack(Item.gunpowder), .75f, 25));
		
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Block.sandStone), new ItemStack(Block.sand, 4), 100, 50));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Block.brick), new ItemStack(Item.brick, 4), 100, 75));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Block.planks), new ItemStack(Item.stick, 5), 100, 50));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Block.cloth), new ItemStack[]{
																new ItemStack(Item.silk, 1),
																new ItemStack(Item.silk, 2),
																new ItemStack(Item.silk, 4)
																}, new float[]{
																25,
																25,
																25
																}, 50));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Block.dirt), new ItemStack[]{
																new ItemStack(Block.cobblestone),
																new ItemStack(Block.stone),
																new ItemStack(Item.coal),
																new ItemStack(Block.oreIron)
																}, new float[]{
																20,
																4,
																0.75f,
																0.25f
																}, 50));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Block.cobblestone), new ItemStack[]{
																new ItemStack(Block.gravel),
																new ItemStack(Item.coal),
																new ItemStack(Block.oreIron),
																new ItemStack(Item.redstone)
																}, new float[]{
																8.5f,
																2.5f,
																2,
																0.5f
																}, 75));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Block.stone), new ItemStack[]{
			                                                    new ItemStack(Block.gravel),
																new ItemStack(Item.ingotIron),
																new ItemStack(Item.redstone)
																}, new float[]{
																50,
																2,
																0.5f
																}, 75));
		
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.pickaxeWood), new ItemStack[]{new ItemStack(Block.planks, 2)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.axeWood), new ItemStack[]{new ItemStack(Block.planks, 2)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.swordWood), new ItemStack[]{new ItemStack(Block.planks, 1)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.hoeWood), new ItemStack[]{new ItemStack(Block.planks, 1)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.pickaxeStone), new ItemStack[]{new ItemStack(Block.cobblestone, 2)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.axeStone), new ItemStack[]{new ItemStack(Block.cobblestone, 2)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.swordStone), new ItemStack[]{new ItemStack(Block.cobblestone, 1)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.hoeStone), new ItemStack[]{new ItemStack(Block.cobblestone, 1)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.pickaxeSteel), new ItemStack[]{new ItemStack(Item.ingotIron, 2)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.axeSteel), new ItemStack[]{new ItemStack(Item.ingotIron, 2)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.swordSteel), new ItemStack[]{new ItemStack(Item.ingotIron, 1)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.hoeSteel), new ItemStack[]{new ItemStack(Item.ingotIron, 1)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.pickaxeGold), new ItemStack[]{new ItemStack(Item.ingotGold, 2)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.axeGold), new ItemStack[]{new ItemStack(Item.ingotGold, 2)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.swordGold), new ItemStack[]{new ItemStack(Item.ingotGold, 1)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.hoeGold), new ItemStack[]{new ItemStack(Item.ingotGold, 1)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.pickaxeDiamond), new ItemStack[]{new ItemStack(Item.diamond, 2)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.axeDiamond), new ItemStack[]{new ItemStack(Item.diamond, 2)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.swordDiamond), new ItemStack[]{new ItemStack(Item.diamond, 1)}, 100, 25));
		grinderRecipes.add(new GrinderRecipe(new ItemStack(Item.hoeDiamond), new ItemStack[]{new ItemStack(Item.diamond, 1)}, 100, 25));
		ExtractorProducts = new GrinderRecipe(null, new ItemStack[]{
																new ItemStack(Item.ingotIron),
																new ItemStack(Item.redstone),
																new ItemStack(Block.obsidian),
																new ItemStack(Item.ingotGold),
																new ItemStack(Item.diamond)
																}, new float[]{
																0.03125f,
																0.0125f,
																0.009375f,
																0.00625f,
																0.003125f
																}, 0);
		
		MeltableBlocks.add(Block.stone);
		MeltableBlocks.add(Block.cobblestone);
		MeltableBlocks.add(Block.gravel);
		MeltableBlocks.add(Block.netherrack);
		MeltableBlocks.add(Block.slowSand);
		
		energyStoreFullTrigger = new TriggerMachines(90, TriggerMachines.TriggerType.EnergyStoreFull);
		energyStoreEmptyTrigger = new TriggerMachines(91, TriggerMachines.TriggerType.EnergyStoreEmpty);
		energyStorePartFilledTrigger = new TriggerMachines(92, TriggerMachines.TriggerType.EnergyStorePartFilled);
		BlastFurnaceFullTrigger = new TriggerMachines(93, TriggerMachines.TriggerType.BlastFurnaceBlockFull);
		BlastFurnaceEmptyTrigger = new TriggerMachines(94, TriggerMachines.TriggerType.BlastFurnaceBlockEmpty);
		BlastFurnacePartFilledTrigger = new TriggerMachines(95, TriggerMachines.TriggerType.BlastFurnaceBlockPartFilled);
		
		directorDisableNorth = new ActionDirector(110, ActionDirector.Mode.disableNorth);
		directorDisableSouth = new ActionDirector(111, ActionDirector.Mode.disableSouth);
		directorDisableEast = new ActionDirector(112, ActionDirector.Mode.disableEast);
		directorDisableWest = new ActionDirector(113, ActionDirector.Mode.disableWest);
		directorEnableNorth = new ActionDirector(114, ActionDirector.Mode.enableNorth);
		directorEnableSouth = new ActionDirector(115, ActionDirector.Mode.enableSouth);
		directorEnableEast = new ActionDirector(116, ActionDirector.Mode.enableEast);
		directorEnableWest = new ActionDirector(117, ActionDirector.Mode.enableWest);
		
		
		ActionManager.registerTriggerProvider(new APSMachineTriggerProvider());
		ActionManager.registerActionProvider(new APSMachineActionProvider());
		
    	//####################Energy Store Kit
    	ModLoader.addRecipe(new ItemStack(module_Core.APSKitItem, 1, EnergyStoreID + ItemMachine), new Object[]{ "owo", "ICI", "oco",
                                                                Character.valueOf('o'), Block.obsidian,
                                                                Character.valueOf('w'), BuildCraftTransport.pipePowerWood,
                                                                Character.valueOf('C'), new ItemStack(module_Core.APSPowerCoreItem, 1, 1),
                                                                Character.valueOf('c'), BuildCraftTransport.pipePowerGold,
                                                                Character.valueOf('I'), Item.ingotIron
                                                                });
    	//####################Energy Store
    	module_Core.AddMetaSubblock(EnergyStoreID, TileEntityEnergyStore.class, "energystore", "Energy Store", APSBlockTypes.Machine, true, false);
    	AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(new ItemStack[] {
                                                                new ItemStack(module_Core.APSKitItem, 1, EnergyStoreID + ItemMachine)},
                                                                7500, 
                                                                new ItemStack(module_Core.APSMetaBlockMachine, 1, EnergyStoreID)
                                                                ));
    	                                                                
    	//####################Energy Director Kit
    	ModLoader.addRecipe(new ItemStack(module_Core.APSKitItem, 1, EnergyDirectorID + ItemMachine), new Object[]{ "owo", "wCw", "owo",
                                                                Character.valueOf('o'), Block.obsidian,
                                                                Character.valueOf('w'), BuildCraftTransport.pipePowerWood,
                                                                Character.valueOf('C'), new ItemStack(module_Core.APSPowerCoreItem, 1, EnergyDirectorID)
                                                                });
    	//####################Energy Director
    	module_Core.AddMetaSubblock(EnergyDirectorID, TileEntityEnergyDirector.class, "energydirector", "Energy Director", APSBlockTypes.Machine, true, false);
    	AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(new ItemStack[] {
                                                                new ItemStack(module_Core.APSKitItem, 1, EnergyDirectorID + ItemMachine)},
                                                                10000, 
                                                                new ItemStack(module_Core.APSMetaBlockMachine, 1, EnergyDirectorID)
                                                                ));
    	                                       
    	//####################Mineral Extractor Kit
    	ModLoader.addRecipe(new ItemStack(module_Core.APSKitItem, 1, ExtractorID + ItemMachine), new Object[]{ "oEo", "BGB", "oCo",
                                                                Character.valueOf('o'), Block.obsidian,
                                                                Character.valueOf('E'), new ItemStack(BuildCraftEnergy.engineBlock, 1, 2),
                                                                Character.valueOf('B'), Item.bucketEmpty,
                                                                Character.valueOf('G'), BuildCraftCore.goldGearItem,
                                                                Character.valueOf('C'), new ItemStack(module_Core.APSPowerCoreItem, 1, 2)
                                                                });
    	//####################Mineral Extractor
    	module_Core.AddMetaSubblock(ExtractorID, TileEntityExtractor.class, "extractor", "Mineral Extractor", APSBlockTypes.Machine);
    	AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(new ItemStack[] {
												                new ItemStack(module_Core.APSKitItem, 1, ExtractorID + ItemMachine)},
												                5000, 
												                new ItemStack(module_Core.APSMetaBlockMachine, 1, ExtractorID)
												                ));
    	
    	//####################Block Grinder Kit
    	ModLoader.addRecipe(new ItemStack(module_Core.APSKitItem, 1, GrinderID + ItemMachine), new Object[]{ "oSo", "G W", "oCo",
                                                                Character.valueOf('o'), Block.obsidian,
                                                                Character.valueOf('S'), BuildCraftTransport.pipeItemsStone,
                                                                Character.valueOf('G'), BuildCraftCore.ironGearItem,
                                                                Character.valueOf('W'), BuildCraftTransport.pipeItemsWood,
                                                                Character.valueOf('C'), new ItemStack(module_Core.APSPowerCoreItem, 1, 1)
                                                                });
    	
    	//####################Block Grinder
    	module_Core.AddMetaSubblock(GrinderID, TileEntityGrinder.class, "blockgrinder", "Block Grinder", APSBlockTypes.Machine);
    	/*ModLoader.addShapelessRecipe(new ItemStack(module_Core.APSMetaBlockMachine, 1, 3), new Object[]{
													    		new ItemStack(module_Core.APSKitItem, 1, GrinderID + ItemMachine),
													    		BuildCraftCore.wrenchItem
													    		});*/
    	AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(new ItemStack[] {
												                new ItemStack(module_Core.APSKitItem, 1, GrinderID + ItemMachine)},
												                5000, 
												                new ItemStack(module_Core.APSMetaBlockMachine, 1, GrinderID)
												                ));
    	
    	//####################Blast Furnace Kit
    	ModLoader.addRecipe(new ItemStack(module_Core.APSKitItem, 1, MagmafierID + ItemMachine), new Object[]{ "oSo", "CFL", "owo",
                                                                Character.valueOf('o'), Block.obsidian,
                                                                Character.valueOf('S'), BuildCraftTransport.pipeItemsStone,
                                                                Character.valueOf('C'), new ItemStack(module_Core.APSPowerCoreItem, 1, 2),
                                                                Character.valueOf('F'), Block.stoneOvenIdle,
                                                                Character.valueOf('L'), BuildCraftTransport.pipeLiquidsGold,
                                                                Character.valueOf('w'), BuildCraftTransport.pipePowerGold
                                                                });
    	//####################Blast Furnace
    	module_Core.AddMetaSubblock(MagmafierID, TileEntityMagmafier.class, "blastfurnace", "Blast Furnace", APSBlockTypes.Machine);
    	AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(new ItemStack[] {
												                new ItemStack(module_Core.APSKitItem, 1, MagmafierID + ItemMachine)},
												                5000, 
												                new ItemStack(module_Core.APSMetaBlockMachine, 1, MagmafierID)
												                ));
    	
    	//####################Powered Furnace Kit
    	ModLoader.addRecipe(new ItemStack(module_Core.APSKitItem, 1, PoweredFurnaceID + ItemMachine), new Object[]{ "oIo", "iFW", "owo",
                                                                Character.valueOf('o'), Block.obsidian,
                                                                Character.valueOf('w'), BuildCraftTransport.pipePowerWood,
                                                                Character.valueOf('i'), BuildCraftCore.ironGearItem,
                                                                Character.valueOf('F'), Block.stoneOvenIdle,
                                                                Character.valueOf('W'), BuildCraftTransport.pipeItemsWood,
                                                                Character.valueOf('I'), Item.ingotIron
                                                                });
    	
    	//####################Powered Furnace
    	module_Core.AddMetaSubblock(PoweredFurnaceID, TileEntityPoweredFurnace.class, "poweredfurnace", "Powered Furnace", APSBlockTypes.Machine);
    	/*ModLoader.addShapelessRecipe(new ItemStack(module_Core.APSMetaBlockMachine, 1, PoweredFurnaceID), new Object[]{
													    		new ItemStack(module_Core.APSKitItem, 1, PoweredFurnaceID + ItemMachine),
													    		BuildCraftCore.wrenchItem
													    		});*/
    	AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(new ItemStack[] {
                                                                new ItemStack(module_Core.APSKitItem, 1, PoweredFurnaceID + ItemMachine)},
                                                                2500, 
                                                                new ItemStack(module_Core.APSMetaBlockMachine, 1, PoweredFurnaceID)
                                                                ));
    	
    	//#################### Energy Crystal
    	
    	ModLoader.addName(module_Core.EnergyCrystal, "Energy Crystal");
    	ModLoader.addRecipe(new ItemStack(module_Core.EnergyCrystal), new Object[]{ " E ", "rer", " b ",
    																	Character.valueOf('E'), module_Core.bottledEnergy,
    																	Character.valueOf('r'), Item.redstone,
    																	Character.valueOf('e'), Item.emerald,
    																	Character.valueOf('b'), Item.bucketWater
    																	});
    	
    	//#################### Energy Siphon kit
    	ModLoader.addRecipe(new ItemStack(module_Core.APSKitItem, 1, EnergySiphonID + ItemMachine), new Object[]{ "iPi", "oEo", "ioi",
    																											Character.valueOf('i'), Item.ingotIron,
    																											Character.valueOf('P'), BuildCraftTransport.pipePowerGold,
    																											Character.valueOf('E'), module_Core.EnergyCrystal,
    																											Character.valueOf('o'), Block.obsidian,
    																											});
    	
    	//#################### Energy Siphon
    	module_Core.AddMetaSubblock(EnergySiphonID, TileEntityEnergySiphon.class, "energysiphon", "Energy Siphon", APSBlockTypes.Machine);
    	
    	AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(new ItemStack[]{
    																		new ItemStack(module_Core.APSKitItem, 1, EnergySiphonID + ItemMachine)},
    																		2000,
    																		new ItemStack(module_Core.APSMetaBlockMachine, 1, EnergySiphonID)
    																		));
    	
	}
	
	@Override
	public void clientInit() { }
	
	public void postInit() {}
	
	public static boolean isMeltable(ItemStack stack)
	{
		if(stack.itemID > 255)
			return false;
		return MeltableBlocks.contains(Block.blocksList[stack.itemID]);
	}
}
